package com.zeyuan.rpcclient.netty.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class PromiseFuture {
    public static void main(String[] args) {
        //准备eventloop
        EventLoop eventLoop = new NioEventLoopGroup().next();
        //主动创建promise
        DefaultPromise<Integer> promise=new DefaultPromise<>(eventLoop);
        new Thread(()->{
            //任意线程执行计算，计算完成后向promise填充结果
            System.out.println("开始计算");
            //计算。。
            //完成计算
            promise.setSuccess(80); //80就是结果
            //promise.setFailure(exception);
        }).start();
        //接收结果
//        Integer integer = promise.get(); //同步
        //异步接收结果 因为是eventloop
        promise.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {

            }
        });
    }
}
